package com.ximalaya.demo;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewpager.widget.PagerAdapter;

/**
 * Time: 2022/3/30 3:04 下午
 * Author: leixun
 * Email: leixun33@163.com
 * <p>
 * Description:
 */
public class LiveCourseViewPagerAdapter extends PagerAdapter {
    private SparseArray<View> views;
    private Context context;
    private int size =0;

    public LiveCourseViewPagerAdapter(Context context,int size)
    {
        this.views = new SparseArray();
        this.context = context;
        this.size = size;
    }


    @Override
    public void destroyItem(ViewGroup paramViewGroup, int paramInt, Object paramObject)
    {
        paramViewGroup.removeView((View)paramObject);
        this.views.delete(paramInt);
    }

    @Override
    public int getCount()
    {
        return size;
    }

    @Override
    public void finishUpdate(ViewGroup container) {
        super.finishUpdate(container);
    }

    public Object instantiateItem(ViewGroup paramViewGroup, int paramInt)
    {
        View childView =null;
        if(this.views.get(paramInt)!=null){
            childView = this.views.get(paramInt);
            if(childView.getParent()!=null){
                ((ViewGroup)childView.getParent()).removeView(childView);
            }
        }else {
            childView = LayoutInflater.from(paramViewGroup.getContext()).inflate(R.layout.live_course_pager_item, paramViewGroup, false);
        }
        if (childView != null)
        {
            this.views.put(paramInt, childView);
        }
        paramViewGroup.addView(childView);
        return childView;
    }

    public boolean isViewFromObject(View paramView, Object paramObject)
    {
        return paramView == paramObject;
    }
}